//
//  LookSleeveViewController.m
//  WhereToTravel
//
//  Created by dlios on 15/10/14.
//  Copyright © 2015年 zhoushengmin. All rights reserved.
//

#import "LookSleeveViewController.h"
#import "SleeveModel.h"
#import "WebViewController.h"
#import "SleeveTableViewCell.h"

@interface LookSleeveViewController ()<UITableViewDataSource,UITableViewDelegate,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property (nonatomic, strong) NSMutableArray *sleeveArr;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation LookSleeveViewController

// 视图的出现将tabbar隐藏
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(night) name:@"night" object:nil];
        [self createData];
    }
    return self;
}

#pragma mark 看锦囊数据解析
- (void)createData {
    NSString *strUrl = @"http://api.breadtrip.com/hunter/products/more/?city_name=全部城市&sign=af2a27ef9ae626ec132d3bb2620c3abd&start=0%20";
    strUrl = [strUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    [ZSMAFNetWork geturl:strUrl body:nil result:ZSMJSON headerFile:nil success:^(id result) {
        
        NSMutableDictionary *strDic = result;
        NSMutableArray *strArr= strDic[@"product_list"];
        self.sleeveArr = [NSMutableArray array];
        for (NSMutableDictionary *dic in strArr) {
            SleeveModel *sleeveModel = [[SleeveModel alloc] init];
            [sleeveModel setValuesForKeysWithDictionary:dic];
            [self.sleeveArr addObject:sleeveModel];
        }
        [self.tableView reloadData];
        
        [self.tableView.header endRefreshing];
    } failure:^(NSError *error) {
        NSLog(@"错误是:%@",error);
        
    }];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100 * WIDTH, 30 * HEIGHT)];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.text = @"看锦囊";
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = self.titleLabel;
    [self createTableView];
    
    // 左上角返回图标
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftAction:)];
}

// 返回
- (void)leftAction:(UIBarButtonItem *)barButton {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark tableView创建
- (void)createTableView {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH * 375, HEIGHT * 554) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.rowHeight = 300 * HEIGHT;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.view);
        
    }];
    
    [self createMJRefresh];
}

// 空白页占位图
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, self.view.bounds.size.height/2, WIDTH*375, 30)];
    label.text = @"! 网速不给力";
    label.textAlignment = NSTextAlignmentCenter ;
    label.textColor = [UIColor redColor];
    return label;
}

- (void)createMJRefresh {
    // 下拉刷新
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(createData)];
    [self.tableView.header beginRefreshing];
    
}

#pragma mark tableView的数据处理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.sleeveArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    SleeveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[SleeveTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:reuse];
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (self.sleeveArr.count > indexPath.row) {
        SleeveModel *sleeveModel = self.sleeveArr[indexPath.row];
        [cell.upBigImageView sd_setImageWithURL:[NSURL URLWithString:sleeveModel.title_page] placeholderImage:[UIImage imageNamed:@"占位图"]];
        [cell.upMissImageView sd_setImageWithURL:[NSURL URLWithString:sleeveModel.user[@"avatar_l"]] placeholderImage:[UIImage imageNamed:@"占位图"]];
        cell.mistLabel.text = sleeveModel.title;
        NSString *strDate = [NSString stringWithFormat:@"日期:%@",sleeveModel.date_str];
        cell.downLeftLabel.text = strDate;
        NSString *strAddress = [NSString stringWithFormat:@"地点:%@",sleeveModel.address];
        cell.downMistLabel.text = strAddress;
        NSString *strLike = [NSString stringWithFormat:@"%@人喜欢",sleeveModel.like_count];
        cell.downRightLabel.text = strLike;
        NSString *strPrice = [NSString stringWithFormat:@"%@元",sleeveModel.price];
        cell.downLabel.text = strPrice;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    WebViewController *webVC = [[WebViewController alloc] init];
    [self.navigationController pushViewController:webVC animated:YES];
    SleeveModel *sleeveModel = self.sleeveArr[indexPath.row];
    NSString *strUrl = [NSString stringWithFormat:@"http://web.breadtrip.com/hunter/product/%@/",sleeveModel.product_id];
    webVC.url = strUrl;
}

- (void)night {
    if ([SingletonNight shareSingletonNight].isSelete == YES) {
        self.tableView.backgroundColor = [UIColor blackColor];
        self.titleLabel.textColor = [UIColor blackColor];
    }
    else {
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.titleLabel.textColor = [UIColor whiteColor];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"night" object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
